#include<QDebug>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFile>
#include <QKeyEvent>
#include<QString>
#include<QIODevice>
#include<QMessageBox>
#include<QCoreApplication>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_ptrStuSql(nullptr)
{
    ui->setupUi(this);
    flag=0;
    pageSum=1;
    //第一页时前一页不显示
    if(pageSum==1)
        ui->btn_prev->setEnabled(false);
    //第一页时后一页显示
    ui->btn_next->setEnabled(true);

    //总页数
    sum=m_ptrStuSql->getStuCnt();
    if(sum%4==0)//12
        sum=sum/4;//3
    else//13
        sum=(sum/4)+1;

    ui->treeWidget->clear();
    ui->treeWidget->setColumnCount(1);
    QStringList l;
    l<<"学生信息管理系统";

    QTreeWidgetItem *pf=new QTreeWidgetItem(ui->treeWidget,l);
    ui->treeWidget->addTopLevelItem(pf);

    l.clear();
    l<<"学生管理";
    QTreeWidgetItem *p1=new QTreeWidgetItem(pf,l);
    pf->addChild(p1);

    l.clear();
    l<<"管理员管理";
    QTreeWidgetItem *p2=new QTreeWidgetItem(pf,l);
    pf->addChild(p2);

    ui->treeWidget->expandAll();
    m_ptrStuSql=stuSql::getinstance();
  //  m_ptrStuSql->init();

    //模拟数据
   m_lNames<<"yc";
   m_lNames<<"lh";
   m_lNames<<"lx";
   m_lNames<<"cgx";
   m_lNames<<"zle";
   m_lNames<<"cyx";
   m_lNames<<"hwy";

   updateTable();

}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::on_btn_simulation_clicked()
{
    QList<StuInfo> l;
    for(int i=0;i<m_lNames.size();i++){
        StuInfo info;
        info.name=m_lNames[i];
        if(i%2)
            info.age=16;
        if(i%3)
            info.age=17;
        if(i%7)
            info.age=18;
        info.grade=7;
        info.uiclass=1;
        info.studentid=20222150;
        info.phone="13872145679";
        info.wechat="13872145679";
        l.append(info);
    }
    m_ptrStuSql->addStu(l);
    updateTable();
}
//添加学生对话框
void MainWindow::on_btn_add_clicked()
{
    //添加
    m_diaAddStu.setType(true);
    //模态对话框
    m_diaAddStu.exec();
    pageSum=sum;
    updateTable();
}

//清空学生表
void MainWindow::on_btn_clear_clicked()
{
    m_ptrStuSql->clearStuTable();
    updateTable();
}
//刷新表格内容
void MainWindow::updateTable()
{
    ui->tableWidget->clear();
    //设置table表头
    ui->tableWidget->setColumnCount(9);
    QStringList l;
    l<<"序号"<<"id"<<"姓名"<<"年龄"<<"年级"<<"班级"<<"学号"<<"电话"<<"微信";
    //设置第一行数据
    ui->tableWidget->setHorizontalHeaderLabels(l);

    //只能选中行 ctrl选择多行
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    //表格无法编辑
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //获取全部学生信息
   // QList<StuInfo> lStudent=m_ptrStuSql->getAllStu();
    QList<StuInfo> lStudent=m_ptrStuSql->getPageStu(pageSum,4);

    //获取学生数量AAA
    ui->lb_mount->setText(QString("学生总数量:%1").arg(m_ptrStuSql->getStuCnt()));
    //设置表格内容
    ui->tableWidget->setRowCount(lStudent.size());
    for(int i=0;i<lStudent.size();i++){
        //分页序号通式
        int k=(i+1)+((pageSum-1)*4);
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(k)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(lStudent[i].id)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(lStudent[i].name));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString::number(lStudent[i].age)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lStudent[i].grade)));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(lStudent[i].uiclass)));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(lStudent[i].studentid)));
        ui->tableWidget->setItem(i,7,new QTableWidgetItem(lStudent[i].phone));
        ui->tableWidget->setItem(i,8,new QTableWidgetItem(lStudent[i].wechat));
    }
    //非第一页则显示上一页按钮
    if(pageSum>1){
        ui->btn_prev->setEnabled(true);
    }
    //非最后一页则显示下一页按钮
    if(pageSum<sum){
        ui->btn_next->setEnabled(true);
    }
}
//删除
void MainWindow::on_btn_del_clicked()
{
    //获取当前行的值
    int i=ui->tableWidget->currentRow();
    if(i>=0){
        //获取第i行第1列的值(id)
        int id=ui->tableWidget->item(i,1)->text().toUInt();
        m_ptrStuSql->delStu(id);
        updateTable();
        QMessageBox::information(nullptr,"信息","删除成功");
    }
}
//修改
void MainWindow::on_bt_update_clicked()
{
    StuInfo info;
    int i=ui->tableWidget->currentRow();
    if(i>=0){
        info.id=ui->tableWidget->item(i,1)->text().toUInt();
        info.name=ui->tableWidget->item(i,2)->text();
        info.age=ui->tableWidget->item(i,3)->text().toUInt();
        info.grade=ui->tableWidget->item(i,4)->text().toUInt();
        info.uiclass=ui->tableWidget->item(i,5)->text().toUInt();
        info.studentid=ui->tableWidget->item(i,6)->text().toUInt();
        info.phone=ui->tableWidget->item(i,7)->text();
        info.wechat=ui->tableWidget->item(i,8)->text();
        m_diaAddStu.setType(false,info);
        m_diaAddStu.exec();
        updateTable();
    }
}
//通过名字搜索
void MainWindow::on_btn_search_clicked()
{
   QString strFilter =ui->le_search->text();
   QList<StuInfo> lStudent=m_ptrStuSql->getStuByName(strFilter);
   qDebug()<<lStudent.size();
    //设置表格内容
    ui->tableWidget->setRowCount(lStudent.size());
    for(int i=0;i<lStudent.size();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(i+1)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString::number(lStudent[i].id)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(lStudent[i].name));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString::number(lStudent[i].age)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lStudent[i].grade)));
        ui->tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(lStudent[i].uiclass)));
        ui->tableWidget->setItem(i,6,new QTableWidgetItem(QString::number(lStudent[i].studentid)));
        ui->tableWidget->setItem(i,7,new QTableWidgetItem(lStudent[i].phone));
        ui->tableWidget->setItem(i,8,new QTableWidgetItem(lStudent[i].wechat));
    }
    ui->btn_prev->setEnabled(false);
    ui->btn_next->setEnabled(false);
    ui->sb_page->setEnabled(false);
}
//上一页
void MainWindow::on_btn_prev_clicked()
{
    pageSum--;
    ui->sb_page->setValue(pageSum);
    if(pageSum==1)
        ui->btn_prev->setEnabled(false);
    updateTable();
}
//下一页
void MainWindow::on_btn_next_clicked()
{
    pageSum++;
    ui->sb_page->setValue(pageSum);
    if(pageSum>=sum){
        //最后一页
        ui->btn_next->setEnabled(false);
    }
    updateTable();
}

void MainWindow::on_btn_exit_clicked()
{
    exit(0);
}

//第几页搜索
void MainWindow::on_sb_page_valueChanged(int arg1)
{
    //页码范围
    ui->sb_page->setRange(1,sum);
    pageSum=arg1;
    if(arg1==1){
        ui->btn_prev->setEnabled(false);
    }
    if(arg1==sum){
        ui->btn_next->setEnabled(false);
    }
    updateTable();

}
